<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>点名</title>
  </head>
  <style>
    * {
      margin: auto;
    }
    body {
      margin: 0 auto;
      text-align: center;
    }
    div {
      width: 300px;
      height: 300px;
      border: 1px solid;
      text-align: center;
      line-height: 300px;
    }
    button {
      background: greenyellow;
      width: 120px;
      height: 50px;
      margin-top: 10px;
    }
  </style>
  <body>
    <h1>1.随机点名</h1>
    <div><span>???</span></div>
    <button onclick="fun2()">开始</button>
  </body>
  <script>
    // var timeObj //全局变量
    function fun() {
      // Math.random() //0-1之间的小数
      var arr = [
        '董阳光',
        '王静允',
        '张洪恩',
        '陈士豪',
        '房  雪',
        '刘金浩',
        '李新杭',
        '郑嘉豪',
        '蔡欣园',
        '何国锋',
        '蔡飞亨',
        '张惠林',
      ]
      var num = Math.floor(Math.random() * arr.length)
      document.getElementsByTagName('span')[0].innerHTML = arr[num]
    }
    var timeObj
    function fun2() {
      var obj = document.getElementsByTagName('button')[0]
      if (obj.innerHTML == '停止') {
        obj.innerHTML = '开始'
        clearInterval(timeObj)
      } else {
        timeObj = setInterval('fun()', 50)
        obj.innerHTML = '停止'
      }
    }
  </script>
</html>
